import javax.swing.*;
import java.util.*;
import java.awt.event.*;

public class Calculadora extends JFrame implements ActionListener {
	
	private JButton arrayBotones [][] = new JButton [3][3];
	private int margenHoriz = 10, margenVert = 10;
	private JButton cero = new JButton();
	private JButton punto = new JButton();
	private JButton suma = new JButton();
	private JButton resta = new JButton();
	private JButton div = new JButton();
	private JButton mult = new JButton();
	private JButton igual = new JButton();
	private JLabel resul = new JLabel();
	private double num1 = 0, num2, result = 0;
	private JTextField num1Text = new JTextField();
	private JTextField num2Text = new JTextField();
	private JTextField pantText = new JTextField();
	private int toca = 1, opcion = 0;
	
	
	public Calculadora () {
		this.setLayout(null);
		this.setVisible(true);
		this.setBounds(400, 400, 400, 250);
		this.colocarBotones(arrayBotones);
		
		cero = new JButton();
		cero.setSize(95, 45);
		cero.setLocation(10, 160);
		cero.setText("0");
		cero.addActionListener(this);
		add(cero);
		
		punto = new JButton();
		punto.setSize(45, 45);
		punto.setLocation(110, 160);
		punto.setText(".");
		punto.addActionListener(this);
		add(punto);
		
		igual.setText("=");
		igual.setSize(95, 45);
		igual.setLocation(250, 160);
		igual.addActionListener(this);
		add(igual);
		
		suma = new JButton();
		suma.setSize(45, 45);
		suma.setLocation(175, 10);
		suma.setText("+");
		suma.addActionListener(this);
		add(suma);
		
		resta = new JButton();
		resta.setSize(45, 45);
		resta.setLocation(175, 60);
		resta.setText("-");
		resta.addActionListener(this);
		add(resta);
		
		div = new JButton();
		div.setSize(45, 45);
		div.setLocation(175, 110);
		div.setText("/");
		div.addActionListener(this);
		add(div);
		
		mult = new JButton();
		mult.setSize(45, 45);
		mult.setLocation(175, 160);
		mult.setText("x");
		mult.addActionListener(this);
		add(mult);
		
		resul.setText("");
		resul.setLocation(275, 50);
		resul.setSize(500, 100);
		add(resul);
		
		num1Text.setLocation(10, 10);
		num1Text.setSize(100, 50);
		//add(num1Text);
		
		num2Text.setLocation(10, 80);
		num2Text.setSize(100, 50);
		//add(num2Text);
		
		add(pantText);
		pantText.setText("0");
		pantText.setLocation(225, 30);
		pantText.setSize(150, 50);
		pantText.setEditable(false);
		
	}	
	
	public void colocarBotones (JButton[][] botones){
		int cont = 1;
		for (int i = 0; i < botones.length; i++) {
			for (int j = 0; j < botones[i].length; j++) {				
				botones[i][j] = new JButton ();
				botones[i][j].addActionListener(this);
				botones[i][j].setSize(45, 45);
				botones[i][j].setLocation(margenHoriz, margenVert);
				botones[i][j].setText(String.valueOf(cont));
				add(botones[i][j]);
				margenHoriz += 50;
				if (j == 3 - 1) {
					margenHoriz = 10;					
				}	
				cont++;
			}
			margenVert += 50;
		}
		margenVert = 10;
		margenHoriz = 10;
	}	
	
	public void actionPerformed (ActionEvent ae) {
		/*
		try {
		num1 = Double.parseDouble(num1Text.getText());
		num2 = Double.parseDouble(num2Text.getText());
		if (e.getSource() == suma) {
			resul.setText(String.valueOf(num1 + num2));
			
		}
		if (e.getSource() == resta) {
			resul.setText(String.valueOf(num1 - num2));
		}
		if (e.getSource() == div) {
			resul.setText(String.valueOf(num1 / num2));
		}
		if (e.getSource() == mult) {
			resul.setText(String.valueOf(num1 * num2));
		}
		}
		catch (Exception a) {
			resul.setText("ERROR");
		}
		*/
		
		for (int i = 0; i < 3; i++) {
			for (int e = 0; e < 3; e++) {
				if (ae.getSource() == arrayBotones[i][e]) {
					if (pantText.getText().equals("0")) {
						pantText.setText("");
					}
					pantText.setText(pantText.getText() + arrayBotones[i][e].getText());
				}
			}
		}
		
		if (ae.getSource() == cero) {
			pantText.setText(pantText.getText() + "0");
		}
		
		if (ae.getSource() == igual) {
			try {
				try {
					num1 = Double.parseDouble(pantText.getText().substring(0, pantText.getText().indexOf(" ")));
				}
				catch (Exception e) {
					try {
						num1 = Double.parseDouble(resul.getText());
					}
					catch (NumberFormatException nfe) {
						num1 = 0;						
					}
				}
				num2 = Double.parseDouble(pantText.getText().substring(pantText.getText().lastIndexOf(" ") + 1));
				try {
					switch (opcion) {
						case 1: result = (float)num1 + num2;
								break;
						case 2: result = (float)num1 - num2;
								break;
						case 3: result = (float)num1 / num2;
								break;
						case 4: result = (float)num1 * num2;
								break;
						default: result = num1;
								break;
					}
					resul.setText(String.valueOf(result));
					pantText.setText("");
				}
				catch (Exception e) {
					resul.setText("ERROR");
				}
				opcion = 0;
				toca = 1;
			}
			catch (Exception e) {
				resul.setText("ERROR");
			}
		}
		
		if (ae.getSource() == punto) {
			pantText.setText(pantText.getText() + ".");
		}
		
		if (ae.getSource() == suma) {
			pantText.setText(pantText.getText() + " + ");
			opcion = 1;
			toca = 2;
		}
		
		if (ae.getSource() == resta) {
			pantText.setText(pantText.getText() + " - ");
			opcion = 2;
			toca = 2;
		}
		
		if (ae.getSource() == div) {
			pantText.setText(pantText.getText() + " / ");
			opcion = 3;
			toca = 2;
		}
		
		if (ae.getSource() == mult) {
			pantText.setText(pantText.getText() + " * ");
			opcion = 4;
			toca = 2;
		}
	}
	
	public static void main(String[] args) {
		Calculadora c1 = new Calculadora();		
	}
}
